//对axios的引入
import axios from "axios";
//对axios进行一些简单的配置
//引入element ui的组件
import { MessageBox, Message } from "element-ui";
// console.log(MessageBox);
// console.log(Message);
//对axios的公共配置

//引入进度条
import nprogress from "nprogress";

let http = axios.create({
  withCredentials: true,
  baseURL: "/api",
  //基础地址
  timeout: 5000,
  //超时设置
});
//请求拦截
http.interceptors.request.use((config) => {
  //发起请求显示进度条
  nprogress.start();
  //config就是请求的对象，里面可以拿到请求地址 参数
  let token = localStorage.getItem("token");
  config.headers.authorization = token;
  //return config 理解为放行
  return config;
});

http.interceptors.response.use((config) => {
  nprogress.done();

  //config是响应对象，里面可以拿到响应内容
  let {
    data: { state, msg, code },
    // 10022登入过期，1004token国企。10026没权限
  } = config;
  let codes = ["1002", "1004", "10026"];
  if (codes.includes(code)) {
    localStorage.removeItem("token");
    location.reload();
  }
  let excludeurl = ["/order/query_order_status"];
  let isexclude = excludeurl.includes(config.config.url);
  if (!state && !isexclude) {
    MessageBox.alert(msg, "错误提示", { type: "error" });
  }

  return config;
});

export default http;
